package com.kangye.enterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.enterprise.entity.Banner;
import com.kangye.enterprise.entity.Items;
import com.kangye.enterprise.entity.User;
import com.kangye.enterprise.mapper.BannersMapper;
import com.kangye.enterprise.service.BannersService;
import com.kangye.enterprise.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: enterprise
 * @description:
 * @author: 索腾龙
 * @create: 2021-06-07 15:10
 **/
@Service
@Transactional
public class BannersServiceImpl implements BannersService {
    //注入mapper接口
    @Autowired
    private BannersMapper bannerMappers;
    //查询广告表
    @Override
    public ViewJsonObject<Banner> selBanner(Integer page, Integer limit) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<Banner> list = bannerMappers.selBanner();
        //创建分页对象,把数据存在pageInfo里
        PageInfo<Banner> pageInfo = new PageInfo<Banner>(list);
        //返回json页面
        return new ViewJsonObject<Banner>(pageInfo.getList(), pageInfo.getTotal());
    }
    //添加广告表
    @Override
    public Integer addBanners(Banner banner) {
        return bannerMappers.addBanners(banner);
    }
    //删除
    @Override
    public ViewJsonObject<Banner> delBanners(Integer id) {
        //接收mapper的接口
        Integer code = bannerMappers.delBanners(id);
        //判断code值,
        if (code>0){
            //如果大于0  向页面返回数据状态码,删除成功
            return new ViewJsonObject<Banner>(0,"删除成功");
        }else {
            //否则 向页面返回数据 删除失败
            return new ViewJsonObject<Banner>(1,"删除失败");
        }
    }
    //根据id 查询广告表
    @Override
    public ViewJsonObject<Banner> selBannersById(Integer id) {
        //用实体类结束
        Banner banner = bannerMappers.selBannersById(id);
        //创建list对象
        List<Banner> list = new ArrayList<>();
        //把数据添加到list里
        list.add(banner);
        //像页面返回数据
        return new  ViewJsonObject<Banner>(list);
    }

    //查询轮播图
    @Override
    public ViewJsonObject<Banner> selIbtu(Integer page, Integer limit) {
        //把当前页和每页显示的条数传进来
        PageHelper.startPage(page, limit);
        //查询用户
        List<Banner> list = bannerMappers.selIbtu();
        //创建分页对象,把数据存在pageInfo里
        PageInfo<Banner> pageInfo = new PageInfo<Banner>(list);
        //返回json页面
        return new ViewJsonObject<Banner>(pageInfo.getList(), pageInfo.getTotal());
    }
    //添加轮播图
    @Override
    public Integer addIbtu(Banner banner) {
        return bannerMappers.addIbtu(banner);
    }
    //修改
    @Override
    public Integer updIbtu(Banner banner) {
        return bannerMappers.updIbtu(banner);
    }
}